<?php
/**
 * Created by PhpStorm.
 * User: mohuani
 * Date: 2019/4/14
 * Time: 22:27
 */

namespace App\Services;


class WeatherByCityNameService extends BaseService
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 根据cityName查询天气，将cityName名转换成cityKey进行查询，节省redis内存
     * @param $cityName
     * @return array
     */
    public function weatherInfoCityName($cityName):array
    {
        $cityListFile = self::$util->getCityLists();

        // 将cityName转换成cityKey进行查询
        $cityKey = '';
        foreach ($cityListFile['xml']['d'] as $cityListFileK => $cityListFileV) {
            if ($cityListFileV['-d2'] == $cityName) {
                $cityKey = $cityListFileV['-d1'];
            }
        }

        if ($cityKey == '') {
            $weatherInfo = [];
        } else {
            $weatherByCityKeyService = new WeatherByCityKeyService();
            $weatherInfo = $weatherByCityKeyService->weatherInfoCityKey($cityKey);
        }

        return $weatherInfo;
    }

}